package com.example.blog.service;

import com.example.blog.mapper.BlogMapper;
import com.example.blog.mapper.UserInfoMapper;
import com.example.blog.model.BlogInfo;
import com.example.blog.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2025-01-05
 * Time: 13:09
 */
@Service
@Slf4j
public class UserService {
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private BlogMapper blogMapper;
    public UserInfo selectByName(String name) {
        return userInfoMapper.selectByName(name);
    }

    public UserInfo selectById(Integer userId) {
        return userInfoMapper.selectById(userId);
    }

    public UserInfo getAuthorInfo(Integer blogId) {
        //1：根据博客ID，获取作者信息
        //2：根据作者ID，获取作者信息
        BlogInfo blogInfo = blogMapper.selectById(blogId);
//        Integer userId = blogInfo.getUserId();//有可能这个blogInfo为null,那这里在去getUserId()就会出现空指针异常，这就不合适了
        if(blogInfo == null && blogInfo.getUserId() <= 0){
            log.info("博客信息不存在或者博客作者信息不合法：blogId:{}",blogId);
            return null;
        }
        return userInfoMapper.selectById(blogInfo.getUserId());
    }
}
